package admin

import (
	"ginBasic/app"
	"ginBasic/app/common/business"
	"ginBasic/app/common/lib"
	"ginBasic/app/common/model"
	"ginBasic/app/common/model/filter"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ManagerController struct {
	BaseController
}

func (con ManagerController) Index(c *gin.Context) {
	c.HTML(http.StatusOK, "admin/manager/index.html", gin.H{})
}

func (con ManagerController) GetList(c *gin.Context) {

	var req *filter.ManagerFilterReq
	reqErr := c.ShouldBind(&req)
	if reqErr != nil {
		con.ShowRes(-1, reqErr.Error(), c)
		return
	}
	managerList, total, resErr := business.Manager.GetUserInfoList(req)
	if resErr != nil {
		con.ShowRes(-1, resErr.Error(), c)
		return
	}
	con.Show(0, total, managerList, "查询成功", c)
	return
}

func (con ManagerController) Add(c *gin.Context) {
	roleList := business.Role.RoleList()
	c.HTML(http.StatusOK, "admin/manager/add.html", gin.H{
		"roleList": roleList,
	})
}

func (con ManagerController) DoAdd(c *gin.Context) {
	if c.Request.Method != "POST" {
		con.ShowRes(-1, "请求方式有误", c)
		return
	}
	var req *model.Manager
	reqErr := c.ShouldBind(&req)
	if reqErr != nil {
		con.ShowRes(-1, reqErr.Error(), c)
		return
	}

	if resE := app.VerifyEmailFormat(req.Email); !resE {
		con.ShowRes(-1, "请输入正确邮箱格式", c)
		return
	}

	if reM := app.VerifyMobileFormat(req.Mobile); !reM {
		con.ShowRes(-1, "请输入正确手机格式", c)
		return
	}

	_, err := business.Manager.Add(req)
	if err != nil {
		con.ShowRes(-1, reqErr.Error(), c)
		return
	} else {
		con.ShowRes(0, "操作成功", c)
		return
	}

}

func (con ManagerController) Edit(c *gin.Context) {
	id, reqErr := lib.Int(c.Query("id"))
	if reqErr != nil {
		con.ShowRes(-1, reqErr.Error(), c)
		return
	}
	userInfo := business.Manager.GetUserInfoById(id)
	roleList := business.Role.RoleList()
	c.HTML(http.StatusOK, "admin/manager/edit.html", gin.H{
		"roleList": roleList,
		"userInfo": userInfo,
	})
}

func (con ManagerController) DoEdit(c *gin.Context) {
	if c.Request.Method != "POST" {
		con.ShowRes(-1, "请求方式有误", c)
	}
	var req *model.Manager
	reqErr := c.ShouldBind(&req)
	if reqErr != nil {
		con.ShowRes(-2, reqErr.Error(), c)
		return
	}

	if resE := app.VerifyEmailFormat(req.Email); !resE {
		con.ShowRes(-1, "请输入正确邮箱格式", c)
		return
	}

	if reM := app.VerifyMobileFormat(req.Mobile); !reM {
		con.ShowRes(-1, "请输入正确手机格式", c)
		return
	}
	_, err := business.Manager.Edit(req)
	if err != nil {
		con.ShowRes(-2, reqErr.Error(), c)
		return
	} else {
		con.ShowRes(0, "操作成功", c)
		return
	}
}

func (con ManagerController) Del(c *gin.Context) {
	ids := c.Query("ids")
	if ids == "" {
		con.ShowRes(-1, "记录ID不能为空", c)
		return
	}
	count, err := business.Manager.Delete(ids)
	if err != nil {
		con.ShowRes(-1, err.Error(), c)
		return
	} else {
		con.ShowRes(0, "成功删除"+lib.String(int(count))+"条数据", c)
		return
	}
}

func (con ManagerController) Status(c *gin.Context) {
	if c.Request.Method != "POST" {
		con.ShowRes(-1, "请求方式有误", c)
		return
	}
	var req *model.Manager
	// 获取参数并验证
	if err := c.ShouldBind(&req); err != nil {
		con.ShowRes(-1, "请求方式有误", c)
		return
	}
	res, msg := business.Manager.UpdateStatus(req)
	if !res {
		con.ShowRes(-1, msg, c)
		return
	} else {
		con.ShowRes(0, msg, c)
		return
	}
}
